import request from '@/utils/request'

let mockSaleList = [
  { id: 1, orderNumber: 'ORD20240301001', customer: '张三', amount: 1234.56, status: '已完成', createTime: '2024-03-01 10:00:00' },
  { id: 2, orderNumber: 'ORD20240301002', customer: '李四', amount: 500.00, status: '待支付', createTime: '2024-03-01 11:30:00' },
  { id: 3, orderNumber: 'ORD20240301003', customer: '王五', amount: 200.00, status: '已取消', createTime: '2024-03-01 14:00:00' }
];

export function getSalesList() {
  // return request({
  //   url: '/sales',
  //   method: 'get',
  //   params
  // })

  // 模拟数据，用于前端开发调试
  // const mockList = [
  //   { id: 1, orderNumber: 'ORD20240301001', customer: '张三', amount: 1234.56, status: '已完成', createTime: '2024-03-01 10:00:00' },
  //   { id: 2, orderNumber: 'ORD20240301002', customer: '李四', amount: 500.00, status: '待支付', createTime: '2024-03-01 11:30:00' },
  //   { id: 3, orderNumber: 'ORD20240301003', customer: '王五', amount: 200.00, status: '已取消', createTime: '2024-03-01 14:00:00' }
  // ]
  // const mockTotal = mockList.length

  return new Promise(resolve => {
    setTimeout(() => {
      resolve({
        code: 20000,
        data: { total: mockSaleList.length, items: mockSaleList }
      })
    }, 500) // 模拟网络延迟
  })
}

export function getSaleDetail(id) {
  return request({
    url: `/sales/${id}`,
    method: 'get'
  })
}

export function createSale(data) {
  return new Promise(resolve => {
    setTimeout(() => {
      const newItem = {
        ...data,
        id: Date.now(),
        orderNumber: data.orderNumber || ('ORD' + Date.now()),
        status: data.status || '待支付',
        createTime: data.createTime || new Date().toISOString().slice(0, 19).replace('T', ' ')
      };
      mockSaleList.unshift(newItem);
      resolve({ code: 20000, data: newItem });
    }, 300);
  });
}

export function updateSaleStatus(id, status) {
  return request({
    url: `/sales/${id}/status`,
    method: 'put',
    data: { status }
  })
}

export function getSalesStatistics() {
  // return request({
  //   url: '/sales/statistics',
  //   method: 'get'
  // })

  // 模拟数据，用于前端开发调试
  const mockStatistics = {
    todaySales: 1234.56,
    monthlySales: 15000.00,
    todayOrders: 10,
    monthlyOrders: 256,
    salesTrend: [800, 900, 1100, 950, 1200, 1500, 1300], // 近7日销售额
    productCategorySales: [
      { name: '食品', value: 3000 },
      { name: '饮料', value: 2000 },
      { name: '日用品', value: 4000 },
      { name: '电子产品', value: 1500 },
      { name: '其他', value: 500 }
    ],
    hotProducts: [
      { rank: 1, name: '商品A', category: '食品', volume: 200, amount: 2100 },
      { rank: 2, name: '商品D', category: '饮料', volume: 150, amount: 450 },
      { rank: 3, name: '商品E', category: '日用品', volume: 100, amount: 1500 }
    ]
  }

  return new Promise(resolve => {
    setTimeout(() => {
      resolve({
        code: 20000,
        data: mockStatistics
      })
    }, 300) // 模拟网络延迟
  })
}

export function getSalesReport(params) {
  return request({
    url: '/sales/report',
    method: 'get',
    params
  })
}

export function exportSalesData(params) {
  return request({
    url: '/sales/export',
    method: 'get',
    params,
    responseType: 'blob'
  })
} 